<script setup>
import GoodsItem from '../Home/components/GoodsItem.vue'
import { ref } from 'vue'
import {
  getSubCategoryListService,
  getSubCategorySortListService
} from '@/apis/category'
import { useRoute } from 'vue-router'
const route = useRoute()
const subCategoryList = ref([])

const getSubCategoryList = async () => {
  const res = await getSubCategoryListService(route.params.id)
  subCategoryList.value = res.data.result
}
getSubCategoryList()
// 准备data数据-分类别
const data = ref({
  categoryId: route.params.id,
  page: 1,
  pageSize: 20,
  sortField: 'publishTime'
})
// tab切换
const activeName = ref('publishTime')
const onChange = (name) => {
  data.value.page = 1
  data.value.sortField = name
  getSubCategorySortList()
}
// 获取子分类列表
const sortList = ref([])
const getSubCategorySortList = async () => {
  const res = await getSubCategorySortListService(data.value)
  sortList.value = res.data.result.items
}
getSubCategorySortList()
// 无限滚动
const disabled = ref(false)
const load = async () => {
  data.value.page++
  const res = await getSubCategorySortListService(data.value)
  sortList.value = [...sortList.value, ...res.data.result.items]
  if (res.data.result.items.length === 0) {
    disabled.value = true
  }
}
</script>

<template>
  <div class="container">
    <!-- 面包屑 -->
    <div class="bread-container">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item
          :to="{ path: `/category/${subCategoryList.parentId}` }"
          >{{ subCategoryList.parentName }}</el-breadcrumb-item
        >
        <el-breadcrumb-item>{{ subCategoryList.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="sub-container">
      <el-tabs v-model="activeName" @tab-change="onChange">
        <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
        <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
        <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
      </el-tabs>
      <div
        class="body"
        v-infinite-scroll="load"
        :infinite-scroll-disabled="disabled"
      >
        <!-- 商品列表-->
        <GoodsItem
          v-for="item in sortList"
          :key="item.id"
          :good="item"
        ></GoodsItem>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.bread-container {
  padding: 25px 0;
  color: #666;
}

.sub-container {
  padding: 20px 10px;
  background-color: #fff;

  .body {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>
